package com.xx.job.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.job.controller.dto.PageDTO;
import com.xx.job.entity.system.SysRole;
import com.xx.job.mapper.SysRoleMapper;
import com.xx.job.service.system.SysRoleDeptService;
import com.xx.job.service.system.SysRoleMenuService;
import com.xx.job.service.system.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangcy
 * @since 2021-01-14
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {


    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysRoleDeptService sysRoleDeptService;

    public SysRole selectByPrimaryKey(Long id){
        return sysRoleMapper.selectById(id);
    }

    public List<SysRole> sysRoleList(){
       return sysRoleMapper.selectList(null);
    }

    @Override
    public Map<String,Object> sysRoleList(PageDTO pageDTO, SysRole sysRole) {
        Map<String,Object> data = new HashMap<>();
        IPage<SysRole> page = new Page<SysRole>(pageDTO.getPageIndex(),pageDTO.getPageSize());
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(sysRole.getRoleName())){
            wrapper.like("role_name",sysRole.getRoleName().trim());
        }
        if(StringUtils.isNotBlank(sysRole.getRoleKey())){
            wrapper.like("role_key",sysRole.getRoleKey().trim());
        }
        if(StringUtils.isNotBlank(sysRole.getStatus())){
            wrapper.like("status",sysRole.getStatus().trim());
        }

        IPage<SysRole> sysRoleIPage = sysRoleMapper.selectPage(page, wrapper);
        data.put("count",sysRoleIPage.getTotal());
        data.put("list",sysRoleIPage.getRecords());
        data.put("pageIndex",pageDTO.getPageIndex());
        data.put("pageSize", pageDTO.getPageSize());
        return data;
    }

    @Override
    @Transient
    public int add(SysRole sysRole) {
        sysRoleMapper.insert(sysRole);
        return sysRoleMenuService.add(sysRole);
    }

    @Override
    public int deleteBatchIds(List<Long> ids) {
        return sysRoleMapper.deleteBatchIds(ids);
    }

    @Override
    @Transient
    public int update(SysRole sysRole) {
        List<Long> menuIds = sysRole.getMenuIds();
        Long roleId = sysRole.getRoleId();
        int i = sysRoleMenuService.deleteByMap(roleId);
        int add = sysRoleMenuService.add(sysRole);
        return sysRoleMapper.updateById(sysRole);
    }

    @Override
    public SysRole select(Long id) {
        SysRole sysRole = sysRoleMapper.selectById(id);
        sysRole.setMenuIds(sysRoleMenuService.selectByRoleId(id));
        return sysRole;
    }

    @Override
    public Object roleDataScope(SysRole sysRole) {
        if(sysRole.getDataScope().equals("2") ){
            sysRoleDeptService.add(sysRole);
        }
        return sysRoleMapper.updateById(sysRole);
    }
}
